import React from 'react';
import { inject, observer} from 'mobx-react';
import {routerShape} from 'react-router';
import {Condition} from '../list/listCondition';
import {WeaErrorPage,WeaTools,WeaTop,WeaSearchGroup,WeaRightMenu,WeaPopoverHrm,WeaSelect} from 'ecCom';
import {Button} from 'antd';
import isEmpty from 'lodash/isEmpty';
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

@inject("cptSearchStore")
@observer
class Search extends React.Component {
    static contextTypes = {
        router: routerShape
    }
    constructor(props) {
        super(props);
    }
    resetHeight(height){
        jQuery(".cpt-query-condition").height(height - 60);
    }
    componentDidMount() {
        const {cptSearchStore,params} = this.props; //isdata  1资产资料  2 资产   
        cptSearchStore.initDatas(params);
    }
    componentWillReceiveProps(nextProps) {
        const keyOld = this.props.location.key;
		const keyNew = nextProps.location.key;
		const paramsOld = this.props.params;
        const paramsNew = nextProps.params;
        if(keyOld !== keyNew || paramsOld !== paramsNew) {
            const {cptSearchStore,params} = nextProps;
            cptSearchStore.initDatas(params);
        }
    }
    shouldComponentUpdate(nextProps, nextState) {
        return true;
    }
    render() {
        const { cptSearchStore } = this.props;
        const { loading,form,isdata} = cptSearchStore;
        let title = "";
        if(isdata == "1"){
            title = getLabel(33182,"查询资产资料");
        }else{
            title = getLabel(16425,"查询资产");
        };
        return (
            <div className='cpt-query'>
            	<WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@i7yg17`} datas={this.getRightMenu()}>
                <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@f2lkct`}
                	title={title}
                	loading={loading}
                	icon={<i className='icon-coms-fa' />}
                    iconBgcolor='#f14a2d'
                    getHeight={this.resetHeight.bind(this)}
                	buttons={[]}
                	buttonSpace={10}
                	showDropIcon={true}
                	dropMenuDatas={this.getRightMenu()} >
                        <div className='cpt-query-condition'>
                            <Condition ecId={`${this && this.props && this.props.ecId || ''}_Condition@bn1b92`} listStore={cptSearchStore} form={form}></Condition>
                        </div>
                        <div className='cpt-query-btns'>
                            {this.getSearchButtons()}
                        </div>
                </WeaTop>
                </WeaRightMenu>
            </div>
        )
    }
    getRightMenu(){
    	let btns = [];
    	btns.push({
            key: "search",
    		icon: <i className='icon-coms-search'/>,
            content:getLabel(197,'搜索'),
            onClick: this.forwardQueryResult.bind(this)
    	});
    	return btns;
    }
    getSearchButtons() {
        const { cptSearchStore } = this.props;
        const {clearFormFields} = cptSearchStore;
        const btnStyle = {borderRadius: 3, height: 28, width: 80}
        return [
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@see68r@search`} type="primary" style={btnStyle} onClick={this.forwardQueryResult.bind(this)}>{getLabel(197,"搜索")}</Button>),
            (<span style={{width:'15px', display:'inline-block'}}></span>),
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@oliv1w@reset`} type="ghost" style={btnStyle} onClick={()=>{clearFormFields();}}>{getLabel(2022,"重置")}</Button>)
        ]
    }
    forwardQueryResult(){
        const { params,cptSearchStore } = this.props;
        const {isdata} = params;   
        const { form,setSelectedTreeKey } = cptSearchStore;
        setSelectedTreeKey&&form&&setSelectedTreeKey("sec_"+form.getFormParams().capitalgroupid);
        const path = "/searchResult?from=search&isdata="+isdata;
        this.context.router.push({
            pathname: "/main/cpt"+path
        });
    }
}

export default WeaTools.tryCatch(React, 
    props => <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@gqgqrn`} msg={ props.error ? props.error : getLabel( 383324,'对不起，该页面异常，请联系管理员' )} />,
    {error: ""}
)(Search);